package com.itbaizhan.jsoup;

import cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException;
import cn.wanghaomiao.xpath.model.JXDocument;
import cn.wanghaomiao.xpath.model.JXNode;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.File;
import java.io.IOException;
import java.util.List;

//XPath :XML路径语言,它是一种用来确定标记语言文档中某部分位置的语言
public class Demo5 {
    public static void main(String[] args) throws IOException, XpathSyntaxErrorException {
        /*导入Xpath的jar包
        * 获取Document 对象
        * 将Document 对象转为JXDocument 对象
        * JXDocument 调用selN（String xpath）,获取List<JXNode>对象
        * 遍历List<JXNode>，调用JXNode 的getElement(),转为Element对象
        * 处理Element对象*/

        String path = Demo5.class.getClassLoader().getResource("com/itbaizhan/xsd/student01.xml").getPath();
        //获取Document 对象
        Document document = Jsoup.parse(new File(path),"utf-8");
        //将Document 对象转为JXDocument 对象
        JXDocument jxDocument = new JXDocument(document);
        //JXDocument 调用selN（String xpath）,获取List<JXNode>对象
        List<JXNode> jxNodes = jxDocument.selN("//name");//获取name标签
       // List<JXNode> jxNodes = jxDocument.selN("//student[@id='baizhan_0001']/age");//拿到baiZhan_0001的年龄
        //遍历List<JXNode>，调用JXNode 的getElement(),转为Element对象
        for(JXNode jxNode:jxNodes){
            Element element = jxNode.getElement();
            //处理Element对象
            System.out.println(element);
        }


    }
}
